ROUND2 equ 10

;  Procedure calculate variable slices  buffer, wich describes depth
;  history of each filled screen pixel. It means pixel, that has not
;  zeroed counter in counter buffer.
;  First stage of slices triangle procedure - 3stencil.inc file

st_sl_var_line:
;----------------------------------------------------
;-------------in xmm0 - lo -> hi z1, z2 -------------
;----------------xmm5 - lo -> hi y_min, y_max, x_min,
;---------------- x_max as dword integers -----------
;--------------- eax - x1 ---------------------------
;--------------- ebx - x2 ---------------------------
;--------------- ecx - y-----------------------------
;--------------- esi - pointer to cnt buff ----------
; -------------- edx - pointer to ptr buff ----------


.slvdz  equ  [ebp-4]
.slvz2  equ  [ebp-8]
.slvz1  equ  [ebp-12]
.slvx2  equ  [ebp-16]
.slvx1  equ  [ebp-20]
.slvptr_buff equ [ebp-24]
.slvcnt_buff equ [ebp-28]
.slvtop_ptr  equ [ebp-32]
.x_max  equ [ebp-34]
.x_min  equ [ebp-36]
.y_max  equ [ebp-38]
.y_min  equ [ebp-40]
;.sli_buff equ [ebp-36]
     mov        edx,[slices_ptrs_buff_ptr]
     mov        esi,[slices_counter_ptr]

     push       ebp
     mov        ebp,esp
     sub        esp,64

     movaps     xmm2,xmm5
     packssdw   xmm2,xmm2
     movlps     .y_min,xmm2

     cmp        cx,.y_min
     jl         .l_quit

     cmp        cx,.y_max   ;[yres_var]
     jge        .l_quit
     movzx      ecx,cx
     mov        .slvcnt_buff,esi
     mov        .slvptr_buff,edx
     cmp        eax,ebx
     je         .l_quit
     jl         .l_ok

     xchg       eax,ebx
     shufps     xmm0,xmm0,11100001b
   .l_ok:

     cmp        ax,.x_max
     jge        .l_quit
     cmp        bx,.x_min
     jle        .l_quit

     movlps     .slvz1,xmm0
     mov        .slvx1,eax
     mov        .slvx2,ebx

     sub        ebx,eax
     movss      xmm0,.slvz2
     cvtsi2ss   xmm1,ebx
     rcpss      xmm1,xmm1
     subss      xmm0,.slvz1
     mulss      xmm0,xmm1
     movss      .slvdz,xmm0

     movzx      edx,word .x_max
     mov        ebx, .slvx1
     cmp        bx, .x_min
     jge        @f              ; clipping
     movzx      eax,word .x_min
     sub        eax,ebx
     cvtsi2ss   xmm2,eax
     mulss      xmm2,.slvdz
     addss      xmm2,.slvz1
     movss      .slvz1,xmm2
     movzx      eax, word .x_min
     mov        dword .slvx1,eax
;     movzx      edx,word[.x_max]
;     sub        edx,1
   @@:
     cmp        .slvx2,edx
     jl         @f
     mov        .slvx2,edx

    @@:
 ;   mov         eax,.x1
 ;   cmp         .x2,eax
 ;   je          .l_quit
     movzx      edx,word[xres_var]
     mov        edi,.slvptr_buff
     mov        esi,[slices_counter_ptr] ; .cnt_buff
     mov        eax,ecx  ; y
     mul        edx
     add        eax,.slvx1
     add        esi,eax
     shl        eax,2
     add        edi,eax

     mov        ecx,.slvx2
     sub        ecx,.slvx1
     movss      xmm2,.slvz1   ; cz

   .ccalc:
     movzx     ebx,byte[esi]
     or        ebx,ebx       ; inpossible ?
     jz        .update_counters
     mov       edx,[edi]
   .next_sli:
     movss     xmm0,xmm2
     cmpltss   xmm0,[edx]
     movd      eax,xmm0
     cmp       eax,-1
     jne       .update_sli
     mov       eax,edx    ; eax - top
    ; now shift dword variable in [edx]  ebx-1   times
     dec       ebx
     jz        .ins_new
     shl       ebx,2
     add       edx,ebx
   @@:
     push      dword[edx-4]
     pop       dword[edx]
     sub       edx,4
     cmp       edx,eax
     jnz       @b
   .ins_new:
     movss     [eax],xmm2  ; insert new value
     jmp       .update_counters
   .update_sli:
     add       edx,4
     dec       ebx
     jnz       .next_sli

 .update_counters:
     add       edi,4
     inc       esi
     addss     xmm2,.slvdz
     loop       .ccalc

  .l_quit:
     mov       esp,ebp
     pop       ebp
ret
